﻿namespace Contoso.ActiveDirectory {

    #region Usings
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    #endregion

    public class BusinessInfo : IBusinessInfo {

        #region Members
        /// <summary>
        /// The continent name
        /// </summary>
        public string Continent { get; private set; }

        /// <summary>
        /// The Division/Unit
        /// </summary>
        public string DivUnit { get; private set; }

        /// <summary>
        /// The Division/Unit/Department (DUD)
        /// </summary>
        public string DivUnitDept { get; private set; }

        /// <summary>
        /// The location PeopleSoft code
        /// </summary>
        public string PSNum { get; private set; }

        /// <summary>
        /// The business region
        /// </summary>
        public string Region { get; private set; }

        /// <summary>
        /// The time zone description
        /// </summary>
        public string TimeZone { get; private set; }

        /// <summary>
        /// The UTC offset
        /// </summary>
        public decimal UTCOffset { get; private set; }

        #region Element tags
        private static readonly string ContinentTag = "Continent=";
        private static readonly string DivUnitTag = "DivUnit=";
        private static readonly string DivUnitDeptTag = "DivUnitDept=";
        private static readonly string PSNumTag = "PSNum=";
        private static readonly string RegionTag = "Region=";
        private static readonly string TimeZoneTag = "TimeZone=";
        private static readonly string UTCOffsetTag = "UTCOffset=";
        #endregion
        #endregion

        #region Constructors
        public BusinessInfo() {
            this.Continent = string.Empty;
            this.DivUnit = string.Empty;
            this.DivUnitDept = string.Empty;
            this.PSNum = string.Empty;
            this.Region = string.Empty;
        }

        public BusinessInfo(IReadOnlyList<string> items)
            : this() {
            if (items == null) {
                throw new ArgumentNullException("items");
            }

            if (items.Count == 0) return;

            #region Continent
            var continentElement = items
                .Where(x => x.StartsWith(ContinentTag))
                .FirstOrDefault();
            if (continentElement != null) {
                continentElement = continentElement.Replace(ContinentTag, string.Empty);
                if (!string.IsNullOrWhiteSpace(continentElement)) {
                    this.Continent = continentElement.Trim();
                }
            }
            #endregion

            #region Div/Unit
            var divUnitElement = items
                .Where(x => x.StartsWith(DivUnitTag))
                .FirstOrDefault();
            if (divUnitElement != null) {
                divUnitElement = divUnitElement.Replace(DivUnitTag, string.Empty);
                if (!string.IsNullOrWhiteSpace(divUnitElement)) {
                    this.DivUnit = divUnitElement.Trim();
                }
            }
            #endregion

            #region Div/Unit/Dept
            var divUnitDeptElement = items
                .Where(x => x.StartsWith(DivUnitDeptTag))
                .FirstOrDefault();
            if (divUnitDeptElement != null) {
                divUnitDeptElement = divUnitDeptElement.Replace(DivUnitDeptTag, string.Empty);
                if (!string.IsNullOrWhiteSpace(divUnitDeptElement)) {
                    this.DivUnitDept = divUnitDeptElement.Trim();
                }
            }
            #endregion

            #region PSNum
            var psNumElement = items
                .Where(x => x.StartsWith(PSNumTag))
                .FirstOrDefault();
            if (psNumElement != null) {
                psNumElement = psNumElement.Replace(PSNumTag, string.Empty);
                if (!string.IsNullOrWhiteSpace(psNumElement)) {
                    this.PSNum = psNumElement.Trim();
                }
            }
            #endregion

            #region Region
            var regionElement = items
                .Where(x => x.StartsWith(RegionTag))
                .FirstOrDefault();
            if (regionElement != null) {
                regionElement = regionElement.Replace(RegionTag, string.Empty);
                if (!string.IsNullOrWhiteSpace(regionElement)) {
                    this.Region = regionElement.Trim();
                }
            }
            #endregion

            #region TimeZone
            var timeZoneElement = items
                .Where(x => x.StartsWith(TimeZoneTag))
                .FirstOrDefault();
            if (timeZoneElement != null) {
                timeZoneElement = timeZoneElement.Replace(TimeZoneTag, string.Empty);
                if (!string.IsNullOrWhiteSpace(timeZoneElement)) {
                    this.Region = timeZoneElement.Trim();
                }
            }
            #endregion

            #region UTCOffset
            var utcOffsetElement = items
                .Where(x => x.StartsWith(UTCOffsetTag))
                .FirstOrDefault();
            if (utcOffsetElement != null) {
                utcOffsetElement = utcOffsetElement.Replace(UTCOffsetTag, string.Empty);
                if (!string.IsNullOrWhiteSpace(utcOffsetElement)) {
                    decimal utcOffset = 0;
                    if (decimal.TryParse(utcOffsetElement, out utcOffset)) {
                        this.UTCOffset = utcOffset;
                    }
                }
            }
            #endregion
        } 
        #endregion

        #region Methods
        public override string ToString() {
            var info = new StringBuilder();

            info.AppendFormat("Continent: {0}; ", this.Continent ?? "NULL");
            info.AppendFormat("DivUnit: {0}; ", this.DivUnit ?? "NULL");
            info.AppendFormat("DivUnitDept: {0}; ", this.DivUnitDept ?? "NULL");
            info.AppendFormat("PSNum: {0}; ", this.PSNum ?? "NULL");
            info.AppendFormat("Region: {0}; ", this.Region ?? "NULL");
            info.AppendFormat("TimeZone: {0}; ", this.TimeZone ?? "NULL");
            info.AppendFormat("UTCOffset: {0}; ", this.UTCOffset);

            return info.ToString();
        } 
        #endregion
    }
}
